package com.mega.ebcs.integration.pool;

import java.util.Properties;

import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;

import com.mega.ebcs.ebcsformatter.FieldFormatter;
import com.mega.ebcs.util.Constant;

public class TimerPoolDataQueueBean {
	private int count;
	private ConsumerTemplate consumer;
	private ProducerTemplate producer;
	private Properties prop;

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	public void setConsumer(ConsumerTemplate consumer) {
		this.consumer = consumer;
	}

	public void setProducer(ProducerTemplate producer) {
		this.producer = producer;
	}

	public void ebcsConsumerPool() throws Exception {
		while (true) {
			Exchange msg = consumer.receive(prop.getProperty("jt400.url.response"));
			if (msg == null) {
				break;
			}

			FieldFormatter ff = new FieldFormatter();

			byte[] byteMsg = (byte[]) msg.getIn().getBody();
			String fileName = ff.getAsciiField(byteMsg, new Long(1), Constant.EXCHANGE_REQUEST, Constant.FIELDNAME_ID);
			String message = ff.packEbcsToAscii(byteMsg, new Long(1), Constant.EXCHANGE_REQUEST);

			producer.sendBodyAndHeader("file:Outbox?fileName=" + fileName, message, "number", count++);
		}
	}
}
